// 设备和浏览器检测工具类
export class DeviceDetector {
  // 检测是否在微信浏览器中打开
  static isWechatBrowser() {
    const userAgent = navigator.userAgent.toLowerCase();
    return /micromessenger/i.test(userAgent);
  }

  // 检测是否为移动设备
  static isMobile() {
    const userAgent = navigator.userAgent.toLowerCase();
    return /mobile|android|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent);
  }

  // 检测设备类型
  static getDeviceType() {
    const userAgent = navigator.userAgent.toLowerCase();

    if (/android/i.test(userAgent)) {
      return 'android';
    } else if (/iphone|ipad|ipod/i.test(userAgent)) {
      return 'ios';
    } else {
      return 'other';
    }
  }

  // 获取浏览器信息
  static getBrowserInfo() {
    const userAgent = navigator.userAgent.toLowerCase();
    let browser = 'unknown';

    if (userAgent.includes('edg')) {
      browser = 'edge';
    } else if (userAgent.includes('chrome')) {
      browser = 'chrome';
    } else if (userAgent.includes('firefox')) {
      browser = 'firefox';
    } else if (userAgent.includes('safari')) {
      browser = 'safari';
    } else if (userAgent.includes('msie') || userAgent.includes('trident')) {
      browser = 'internet explorer';
    } else if (userAgent.includes('micromessenger')) {
      browser = 'wechat';
    }

    return browser;
  }

  // 获取手机型号信息
  static getPhoneModel() {
    const userAgent = navigator.userAgent.toLowerCase();
    const deviceType = this.getDeviceType();

    // 如果不是移动设备，直接返回unknown
    if (!this.isMobile()) {
      return {
        brand: 'unknown',
        model: 'unknown',
        fullModel: 'unknown'
      };
    }

    let brand = 'unknown';
    let model = 'unknown';
    let fullModel = 'unknown';

    // 检测iOS设备
    if (deviceType === 'ios') {
      brand = 'Apple';
      // iOS设备的具体型号信息在userAgent中不明确，通常需要额外的方法获取
      // 这里我们可以尝试从userAgent中提取设备标识符
      const deviceInfo = this.getIOSDeviceInfo(userAgent);
      return deviceInfo;
    }

    // 检测Android设备
    if (deviceType === 'android') {
      // 检测常见的Android手机品牌
      if (/samsung/i.test(userAgent)) {
        brand = 'Samsung';
        model = this.extractSamsungModel(userAgent);
      } else if (/huawei|honor/i.test(userAgent)) {
        brand = 'Huawei';
        model = this.extractHuaweiModel(userAgent);
      } else if (/xiaomi|redmi/i.test(userAgent)) {
        brand = 'Xiaomi';
        model = this.extractXiaomiModel(userAgent);
      } else if (/oppo/i.test(userAgent)) {
        brand = 'OPPO';
        model = this.extractOppoModel(userAgent);
      } else if (/vivo/i.test(userAgent)) {
        brand = 'vivo';
        model = this.extractVivoModel(userAgent);
      } else if (/oneplus|1\+/i.test(userAgent)) {
        brand = 'OnePlus';
        model = this.extractOnePlusModel(userAgent);
      } else if (/meizu/i.test(userAgent)) {
        brand = 'Meizu';
        model = this.extractMeizuModel(userAgent);
      }

      fullModel = `${brand} ${model}`.trim();
    }

    return {
      brand,
      model,
      fullModel
    };
  }

  // 获取iOS设备信息
  static getIOSDeviceInfo(userAgent) {
    let deviceInfo = {
      brand: 'Apple',
      model: 'iPhone',
      fullModel: 'Apple iPhone'
    };

    // 在网页环境中，iOS设备的具体型号信息在userAgent中不直接暴露
    // 但我们可以通过一些特征来判断大致类型
    if (/ipad/i.test(userAgent)) {
      deviceInfo.model = 'iPad';
      deviceInfo.fullModel = 'Apple iPad';
    } else if (/ipod/i.test(userAgent)) {
      deviceInfo.model = 'iPod';
      deviceInfo.fullModel = 'Apple iPod';
    }

    // 注意：在网页环境中获取iOS设备的具体型号（如iPhone 14 Pro Max）
    // 通常需要使用更高级的技术，如WebKit的deviceMemory、navigator.maxTouchPoints等
    // 结合已知的设备数据库进行匹配

    return deviceInfo;
  }

  // 提取Samsung设备型号
  static extractSamsungModel(userAgent) {
    const matches = userAgent.match(/samsung[-_]?(.*?)(?=[;\s])/i);
    if (matches && matches[1]) {
      return matches[1].replace(/_/g, ' ');
    }
    return 'Galaxy';
  }

  // 提取Huawei设备型号
  static extractHuaweiModel(userAgent) {
    const matches = userAgent.match(/huawei[-_]?(.*?)(?=[;\s])/i);
    if (matches && matches[1]) {
      return matches[1].replace(/_/g, ' ');
    }
    return 'Device';
  }

  // 提取Xiaomi设备型号
  static extractXiaomiModel(userAgent) {
    const matches = userAgent.match(/(redmi|xiaomi)[-_]?(.*?)(?=[;\s])/i);
    if (matches && matches[2]) {
      return (matches[1] === 'redmi' ? 'Redmi ' : '') + matches[2].replace(/_/g, ' ');
    }
    return 'Mi';
  }

  // 提取OPPO设备型号
  static extractOppoModel(userAgent) {
    const matches = userAgent.match(/oppo[-_]?(.*?)(?=[;\s])/i);
    if (matches && matches[1]) {
      return matches[1].replace(/_/g, ' ');
    }
    return 'Device';
  }

  // 提取vivo设备型号
  static extractVivoModel(userAgent) {
    const matches = userAgent.match(/vivo[-_]?(.*?)(?=[;\s])/i);
    if (matches && matches[1]) {
      return matches[1].replace(/_/g, ' ');
    }
    return 'Device';
  }

  // 提取OnePlus设备型号
  static extractOnePlusModel(userAgent) {
    const matches = userAgent.match(/(oneplus|1\+)[-_]?(.*?)(?=[;\s])/i);
    if (matches && matches[2]) {
      return matches[2].replace(/_/g, ' ');
    }
    return 'OnePlus';
  }

  // 提取Meizu设备型号
  static extractMeizuModel(userAgent) {
    const matches = userAgent.match(/meizu[-_]?(.*?)(?=[;\s])/i);
    if (matches && matches[1]) {
      return matches[1].replace(/_/g, ' ');
    }
    return 'Device';
  }
}